package com.examination.controller.safetyModel;

import com.examination.dao.safetyModel.RiskDao;
import com.examination.entity.Knowledge;
import com.examination.entity.Questions;
import com.examination.entity.Risk;
import com.examination.entity.Specialty;
import com.examination.service.safetyModel.RiskService;
import com.examination.util.JsonResult;
import com.examination.util.MyException;
import com.examination.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "隐患风险类型信息")
@RestController
@RequestMapping("risk")
@CrossOrigin
public class RiskController {
    @Autowired
    private RiskService riskService;

    @ApiOperation(value = "增加风险类型信息" , notes = "测试数据:")
    @PostMapping("/insertRisk")
    public JsonResult insertRisk(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = riskService.insertRisk(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("增加成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("增加失败!");
            return jsonResult;
        }

    }

    @ApiOperation(value = "删除风险类型信息" , notes = "测试数据:")
    @PostMapping("/deleteRisk")
    public JsonResult deleteRisk(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = riskService.deleteRisk(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "修改风险类型信息" , notes = "测试数据:")
    @PostMapping("/updateRisk")
    public JsonResult updateRisk(@RequestBody Map map) {
        System.out.println(map);
        JsonResult jsonResult = new JsonResult();
        int i = riskService.updateRisk(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("修改成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "分页模糊查询风险类型信息" , notes = "测试数据:{\"pageNo\": 1, \"pageSize\":10}")
    @PostMapping("/findRisk")
    public Page<Risk> findRisk(@RequestBody Map map) {
        Page<Risk> page = new Page<Risk>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(riskService.total(map));
        page.setItems(riskService.findRisk(map));
        return page;
    }

    @ApiOperation(value = "下拉框查询风险类型信息" , notes = "测试数据:")
    @PostMapping("/selectRisk")
    public List<Risk> selectRisk(@RequestBody Map map) {
        return riskService.selectRisk(map);
    }

    @ApiOperation(value = "批量导入风险源类型",notes = "")
    @PostMapping("/insertRiskPl")
    public boolean insertRiskPl(@RequestParam("file") MultipartFile file, Integer user_id) throws Exception {
        boolean notNull = false;
        String fileName = file.getOriginalFilename();
        List<Risk> riskList = new ArrayList<Risk>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new MyException("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if (sheet != null) {
            notNull = true;
        }

        Risk risk = null;
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            risk = new Risk();
            //转成string格式
            row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);

            //读取
            String unitProject = row.getCell(0).getStringCellValue();//单位工程名称
            String riskDescription = row.getCell(1).getStringCellValue();//具体风险描述
            String reason = row.getCell(2).getStringCellValue();//产生原因
            String riskCategory = row.getCell(3).getStringCellValue();//风险源类别
            String riskGrade = row.getCell(4).getStringCellValue();//风险等级
            String construction = row.getCell(5).getStringCellValue();//控制设施
            String constructionPerson = row.getCell(6).getStringCellValue();//责任人
            String constructionPhone = row.getCell(7).getStringCellValue();//责任人电话
            String remark = row.getCell(8).getStringCellValue();//备注

            //添加到实体类
            risk.setUser_id(user_id);
            risk.setUnitProject(unitProject);
            risk.setRiskDescription(riskDescription);
            risk.setReason(reason);
            risk.setRiskCategory(riskCategory);
            risk.setRiskGrade(riskGrade);
            risk.setConstruction(construction);
            risk.setConstructionPerson(constructionPerson);
            risk.setConstructionPhone(constructionPhone);
            risk.setRemark(remark);
            System.out.println("risk:"+risk.toString());
            //添加到数组
            riskList.add(risk);

        }
        //批量导入
        for (Risk riskL : riskList) {
            riskService.insertRiskPl(riskL);
        }
        return notNull;
    }
}
